package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台管理中心
 * 商品管理
 * 分类管理
 * 品牌管理
 *
 */
@RestController
@RequestMapping("admin/product")
@Api(tags = {"后台管理中心"})
//@CrossOrigin(origins = {"http://api.gmall.com/"}) //表示跨域  所有请求都能跨域
//@CrossOrigin(methods = {RequestMethod.DELETE})
//@CrossOrigin
public class ManageController {

    @Autowired //自动注入 自动装配
    private ManageService manageService;

    //获取一级分类
    @ApiOperation(value = "获取一级分类")
    @GetMapping("getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> category1List = manageService.getCategory1();
        return Result.ok(category1List);
    }
    //获取二级分类
    @ApiOperation(value = "获取二级分类")
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable(name = "category1Id") Long category1Id ){
        List<BaseCategory2> category2List = manageService.getCategory2(category1Id);
        return Result.ok(category2List);
    }
    //获取三级分类
    @ApiOperation(value = "获取三级分类")
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable(name = "category2Id") Long category2Id ){
        List<BaseCategory3> category3List = manageService.getCategory3(category2Id);
        return Result.ok(category3List);
    }
    //获取平台属性集合 根据一二三级分类Id
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
                @PathVariable(name = "category1Id") Long category1Id,
                @PathVariable(name = "category2Id") Long category2Id,
                @PathVariable(name = "category3Id") Long category3Id
    ){
        return Result.ok(manageService.getBaseAttrInfo(category1Id,category2Id,category3Id));
    }
    //添加平台属性
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        //保存
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }
    //获取品牌列表查询    limit 每页数  page:当前页
    @GetMapping("baseTrademark/{page}/{limit}")
    public Result baseTrademark(
            @PathVariable(name = "page") Integer page,
            @PathVariable(name = "limit") Integer limit
                                ){
        //分页对象
        IPage iPage =  manageService.baseTrademark(page,limit);
        return Result.ok(iPage);
    }
    //商品分页查询
    @GetMapping("/{page}/{limit}")
    public Result productPage(
            @PathVariable(name = "page") Integer page,
            @PathVariable(name = "limit") Integer limit,
            @RequestParam(name = "category3Id")  Long category3Id

    ){
        IPage iPage = manageService.productPage(page,limit,category3Id);
        return Result.ok(iPage);
    }

    //商品添加之品牌下拉框
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){

        List<BaseTrademark> baseTrademarkList =  manageService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

   // 获取销售属性列表
     @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){

        List<BaseSaleAttr> baseSaleAttrList = manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
     }

     //保存Spu
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //保存四张表
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }
    //查询Sku分页对象
    @GetMapping("/list/{page}/{limit}")
    public Result list(
            @PathVariable(name = "page") Integer page,
            @PathVariable(name = "limit") Integer limit

    ){
        IPage iPage = manageService.list(page,limit);
        return Result.ok(iPage);
    }

    //根据spuId 查询所有图片
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable(name = "spuId") Long spuId){
        List<SpuImage> spuImageList = manageService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //查询销售属性及值
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable(name = "spuId") Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = manageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
    //保存Sku
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }
    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable(name = "skuId") Long skuId){
        manageService.onSale(skuId);
        return Result.ok();
    }
    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable(name = "skuId") Long skuId){
        manageService.cancelSale(skuId);
        return Result.ok();
    }


}
